package com.acwing.partition2;

import java.io.*;

/**
 * 索引位置表
 *       0     1
 *       2     3
 * 4  5  6  7  8  9  10
 *       11    12
 * 13 14 15 16 17 18 19
 *       20    21
 *       22    23
 * @author `RKC`
 * @date 2022/1/4 15:47
 */
public class AC181回转游戏idaStar {

    private static final int N = 24;
    private static int[] nums = new int[N];
    //operation[i][j]表示i操作，在i操作中的下标j实际在nums中的位置
    private static int[][] operation = {{0, 2, 6, 11, 15, 20, 22}, {1, 3, 8, 12, 17, 21, 23},
            {10, 9, 8, 7, 6, 5, 4}, {19, 18, 17, 16, 15, 14, 13}, {23, 21, 17, 12, 8, 3, 1},
            {22, 20, 15, 11, 6, 2, 0}, {13, 14, 15, 16, 17, 18, 19}, {4, 5, 6, 7, 8, 9, 10}};
    //表示中间8个元素在nums中的索引
    private static int[] center = {6, 7, 8, 11, 12, 15, 16, 17};
    //逆操作的对应索引
    private static int[] opposite = {5, 4, 7, 6, 1, 0, 3, 2};
    private static int[] path = new int[100];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            String[] s = reader.readLine().split("\\s+");
            if (s.length == 1 && Integer.parseInt(s[0]) == 0) break;
            for (int i = 0; i < s.length; i++) nums[i] = Integer.parseInt(s[i]);
            int depth = 0;
            while (!idaStar(0, depth, -1)) depth++;
            if (depth == 0) writer.write("No moves needed\n");
            else {
                for (int i = 0; i < depth; i++) writer.write((char) ('A' + path[i]));
                writer.write("\n");
            }
            //随便输出一个在中间的数字
            writer.write(nums[center[0]] + "\n");
        }
        writer.flush();
    }

    private static boolean idaStar(int depth, int maxDepth, int last) {
        if (depth + eval() > maxDepth) return false;
        if (eval() == 0) return true;
        for (int i = 0; i < operation.length; i++) {
            //不能进行一次正向操作和逆向操作
            if (last != opposite[i]) {
                //执行一次i操作，并记录该深度是由上一个状态的具体操作
                operate(i);
                path[depth] = i;
                if (idaStar(depth + 1, maxDepth, i)) return true;
                //恢复现场
                operate(opposite[i]);
            }
        }
        return false;
    }

    private static void operate(int x) {
        //进行一次i操作，得到最前的那个数，并把后面的数字往前移动，最后把保存的数字放在最后
        int temp = nums[operation[x][0]];
        for (int i = 0; i < 6; i++) nums[operation[x][i]] = nums[operation[x][i + 1]];
        nums[operation[x][6]] = temp;
    }

    private static int eval() {
        //找到中间8个数出现次数最多的数字
        int[] count = new int[4];
        for (int i = 0; i < center.length; i++) count[nums[center[i]]]++;
        int num = 0;
        for (int i = 1; i <= 3; i++) num = Math.max(num, count[i]);
        return 8 - num;
    }
}
